﻿Imports System.IO
Imports X360
Module BinaryReadWrite

    Friend DeviceIDRead As String = ""
    Friend ProfileIDRead As String = ""
    Friend ConsoleIDRead As String = ""
    Friend FileTypeRead As String = ""
    Friend FileType As String = ""

    Friend Sub GetTypeFile()

        Dim offset As Integer = 836

        Dim sr As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))
        sr.BaseStream.Seek(offset, SeekOrigin.Begin)
        FileTypeRead = Hex(System.Net.IPAddress.HostToNetworkOrder(sr.ReadInt32()))
        sr.Close()

        Select Case FileTypeRead
            Case Is = "1"
                FileType = "Saved Game"
            Case Is = "10000"
                FileType = "Profile"
            Case "D0000"
                FileType = "Arcade"
            Case "9000"
                FileType = "Avatar Item"
            Case "40000"
                FileType = "Cache"
            Case "2000000"
                FileType = "Community Game"
            Case "80000"
                FileType = "Game Demo"
            Case "20000"
                FileType = "Gamer Picture"
            Case "A0000"
                FileType = "Game Title"
            Case "C0000"
                FileType = "Game Trailer"
            Case "400000"
                FileType = "Game Video"
            Case "4000"
                FileType = "Installed Game"
            Case "B0000"
                FileType = "Installer"
            Case "2000"
                FileType = "IPTV Pause Buffer"
            Case "F0000"
                FileType = "License Store"
            Case "2"
                FileType = "Market Place Content"
            Case "100000"
                FileType = "Movie"
            Case "300000"
                FileType = "Music Video"
            Case "500000"
                FileType = "Podcast Video"
            Case "3"
                FileType = "Publisher"
            Case "50000"
                FileType = "Storage Download"
            Case "30000"
                FileType = "Thematic Skin"
            Case "200000"
                FileType = "TV"
            Case "90000"
                FileType = "Video"
            Case "600000"
                FileType = "Viral Video"
            Case "70000"
                FileType = "Xbox Download"
            Case "5000"
                FileType = "Xbox Original Game"
            Case "60000"
                FileType = "Xbox Saved Game"
            Case "1000"
                FileType = "Xbox 360 Title"
            Case "5000"
                FileType = "Xbox Title"
            Case "E0000"
                FileType = "XNA"
            Case Else
                FileType = "Unknown"
        End Select
    End Sub



    Friend Sub GetFilename()
        Try
            Dim scc As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))
            SaveFileByte = ""
            SaveFile = ""
            Dim offset1 As Integer = 1042
            'get game name
            Do Until SaveFileByte = "0" Or SaveFileByte = "00"

                scc.BaseStream.Seek(offset1, SeekOrigin.Begin)
                SaveFileByte = Hex(scc.ReadByte())
                If GameNameByte = "0" Or GameNameByte = "00" Then
                    Exit Do
                Else
                    SaveFile = SaveFile & SaveFileByte
                End If
                offset1 += +2
            Loop

            scc.Close()
            Dim test As String
            Dim l As Integer
            Dim sc As New System.Text.StringBuilder()
            For l = 1 To SaveFile.Length - 1 Step 2
                sc.Append(Convert.ToChar(Convert.ToInt32(Mid(SaveFile, l, 2), 16)))
            Next l
            SaveFile = sc.ToString().Replace("""", "")

        Catch ex As Exception

        End Try

    End Sub

    Friend Sub GetGameName()
        Try

            Dim xPackage As New X360.STFS.STFSPackage(Main.STFSFilePath, Nothing)
            Dim a As X360.STFS.ProfilePackage = New X360.STFS.ProfilePackage(xPackage)

            If a.Header.ThisType = STFS.PackageType.Profile Then
                GameName = a.UserFile.GetGamertag
                a.CloseIO()
            Else
                a.CloseIO()
                Dim sr As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))
                sr.BaseStream.Seek(556, SeekOrigin.Begin)
                FlagFind = System.Net.IPAddress.HostToNetworkOrder(sr.ReadInt64())

                Flag = Hex(FlagFind)

                GameNameByte = ""
                GameName = ""
                Dim offset As Integer = 5778
                'get game name
                Do Until GameNameByte = "0" Or GameNameByte = "00"

                    sr.BaseStream.Seek(offset, SeekOrigin.Begin)
                    GameNameByte = Hex(sr.ReadByte())
                    If GameNameByte = "0" Or GameNameByte = "00" Then
                        Exit Do
                    Else
                        GameName = GameName & GameNameByte
                    End If
                    offset += +2
                Loop
                sr.Close()

                Dim p As Integer
                Dim sb As New System.Text.StringBuilder()
                For p = 1 To GameName.Length Step 2
                    sb.Append(Convert.ToChar(Convert.ToInt32(Mid(GameName, p, 2), 16)))
                Next p
                GameName = sb.ToString().Replace("""", "")
            End If
            
        Catch ex As Exception

        End Try

    End Sub

    Friend Sub GetProfileID()

        Try
            Dim sr As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))
            ProfileIDRead = ""
            Dim offset As Integer = 881
            'get ProfileID

            sr.BaseStream.Seek(offset, SeekOrigin.Begin)
            ProfileIDRead = Hex(System.Net.IPAddress.HostToNetworkOrder(sr.ReadInt64()))

            offset += +1
            sr.Close()
        Catch ex As Exception

        End Try
    End Sub

    Friend Sub GetDeviceID()
        Try
            DeviceIDRead = ""
            Dim DeviceIDByte As String = ""
            Dim sr As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))


            Dim offset As Integer = 1021
            'get Device ID
            Do Until DeviceIDByte = "0" Or DeviceIDByte = "00"

                sr.BaseStream.Seek(offset, SeekOrigin.Begin)
                DeviceIDByte = Hex(sr.ReadByte())
                If DeviceIDByte.Length = 1 Then
                    DeviceIDByte = "0" & DeviceIDByte
                End If
                If DeviceIDByte = "0" Or DeviceIDByte = "00" Then
                    Exit Do
                Else
                    If DeviceIDByte.Length = 1 Then
                        DeviceIDByte = "0" & DeviceIDByte
                    End If
                    DeviceIDRead = DeviceIDRead & DeviceIDByte
                End If
                offset += +1
            Loop
            sr.Close()
        Catch ex As Exception

        End Try

    End Sub
    Friend Sub GetConsoleID()
        Try
            ConsoleIDRead = ""
            Dim ConsoleIDByte As String
            Dim sr As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))


            Dim offset As Integer = 876
            ''get Device ID
            Dim offsetcount As Integer = 0
            Do Until offsetcount = 5
                sr.BaseStream.Seek(offset, SeekOrigin.Begin)
                ConsoleIDByte = Hex(sr.ReadByte())
                If ConsoleIDByte.Length = 1 Then
                    ConsoleIDByte = "0" & ConsoleIDByte
                End If
                ConsoleIDRead = ConsoleIDRead & ConsoleIDByte
                offset += +1
                offsetcount += 1
            Loop
            sr.Close()

        Catch ex As Exception

        End Try

    End Sub

    Friend Sub WriteProfileID()

        Dim offset As Integer = 881
        Dim fs As New FileStream(Main.STFSFilePath, FileMode.Open, FileAccess.ReadWrite)
        fs.Seek((offset), SeekOrigin.Begin)
        Dim i As Integer = 0
        Dim length As Integer = Main.ProfileID.Length

        Do While i <> length
            Dim Bytestring = "&H" & Main.ProfileID.Substring(i, 2)
            fs.WriteByte(Bytestring)

            i += 2
        Loop
        fs.Close()
    End Sub
    Friend Sub WriteConsoleID()
        Try
            Dim offset As Integer = 876
            Dim offset1 As Integer = 6
            Dim i As Integer = 0
            Dim length As Integer = Main.ConsoleID.Length
            Dim ByteString As String

            i = 0
            ByteString = ""
            Dim fr As New FileStream(Main.STFSFilePath, FileMode.Open, FileAccess.ReadWrite)
            fr.Seek((offset), SeekOrigin.Begin)
            Do While i <> length

                ByteString = "&H" & Main.ConsoleID.Substring(i, 2)
                fr.WriteByte(ByteString)

                i += 2
            Loop
            fr.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Friend Sub WriteDeviceID()

        Dim offset As Integer = 1021
        Dim fs As New FileStream(Main.STFSFilePath, FileMode.Open, FileAccess.ReadWrite)
        fs.Seek((offset), SeekOrigin.Begin)
        Dim i As Integer = 0
        Dim length As Integer = Main.DeviceID.Length

        Do While i <> length
            Dim Bytestring = "&H" & Main.DeviceID.Substring(i, 2)
            fs.WriteByte(Bytestring)

            i += 2
        Loop
        fs.Close()
    End Sub

    Friend Sub ReadIDs()
        Try
            GetDeviceID()
            GetProfileID()
            GetConsoleID()

            If DeviceIDRead <> "" Then
                Main.DeviceID = DeviceIDRead

            Else
                MessageBox.Show("DeviceID not found", "DeviceID Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            If ProfileIDRead <> "" Then
                Main.ProfileID = ProfileIDRead

            Else
                MessageBox.Show("ProfileID not found", "ProfileID Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            If ConsoleIDRead <> "" Then
                Main.ConsoleID = ConsoleIDRead

            Else
                MessageBox.Show("ConsoleID not found", "ConsoleID Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show("Error reading ID's from file" & Environment.NewLine & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Module
